<?php

namespace App\Http\Controllers\Manage;


use App\Http\Model\SystemConfigModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\View\View;

class SystemController extends BasicController
{
    //

    public function systemConfigView(): View
    {
        $cacheData = $this->cacheSystemConfig();
        return view($this->viewPath . "system.config_value", ["cacheData" => $cacheData]);
    }

    public function systemConfigTableData(Request $request): array|string
    {
        $data = $request->all();
        if (!isset($data["code"])) {
            return errorReturn("未选择标签");
        }
        $cacheData = $this->cacheSystemConfig();
        return successReturn("获取成功", $cacheData[$data["code"]]["children"]);
    }

    /**
     * 字段状态切换
     * <p>
     *     1. 获取字段内容
     *     2. 修改字段状态
     *     3. 删除缓存
     * </p>
     * @param Request $request
     * @return array|string
     */
    public function systemConfigStatusAction(Request $request): array|string
    {
        $data = $request->all();
        if (!isset($data["code"])) {
            return errorReturn("未选择标签");
        }
        $has = SystemConfigModel::where([
            ['code', '=', $data["code"]],
        ])->first();
        if (!$has)
        {
            return errorReturn("字段不存在");
        }
        $loginInfo = $this->getLoginInfo($request);
        $admin = $loginInfo['admin'];
        $role = $loginInfo['role'];
        $update = (new SystemConfigModel)->updateInfo(dataToWhere([
            "code"=>$data["code"],
            "version"=>$has->version,
        ]), [
            "status"=>$has->status == 1 ? 0: 1,
            "uLtaCode"=>$admin->code,
            "uLtarCode"=>$role->code,
        ]);
        if ($update)
        {
            Cache::delete("system_config_cache");
            return successReturn("操作成功");
        }
        return errorReturn("操作失败");

    }

    /**
     * 添加字段
     * <p>
     *     1. 验证标签组是否存在
     *     2. 验证字段编码和字段名称是否存在
     *     3. 保存字段到数据库
     *     4. 删除缓存
     * </p>
     * @param Request $request
     * @return array|string
     */
    public function systemConfigAddAction(Request $request): array|string
    {
        $data = $request->all();
        $ConfigGroup = (new SystemConfigModel)->hasData(dataToWhere([
            "ltsc003"=>$data["ltsc003"],
            "ltsc004"=>$data["ltsc004"],
        ]));
        if (!$ConfigGroup) {
            return errorReturn("未找到标签组");
        }
        $isCodeHas = (new SystemConfigModel)->hasData(dataToWhere([
            "code"=>strtolower($data["ltsc003"])."_".strtolower($data["code"]),
        ]));
        if ($isCodeHas) {
            return errorReturn("字段已存在");
        }
        $isLTSD001Has = (new SystemConfigModel)->hasData(dataToWhere([
            "ltsc001"=>$data["ltsc001"],
            "ltsc003"=>$data["ltsc003"],
            "ltsc004"=>$data["ltsc004"],
        ]));
        if ($isLTSD001Has) {
            return errorReturn("分组下字段已存在");
        }
        $loginInfo = $this->getLoginInfo($request);
        $admin = $loginInfo['admin'];
        $role = $loginInfo['role'];
        $ConfigId = (new SystemConfigModel)->createInfo([
            "code"=>strtolower($data["ltsc003"])."_".strtolower($data["code"]),
            "ltsc001"=>$data["ltsc001"],
            "ltsc002"=>$data["ltsc002"],
            "ltsc003"=>$data["ltsc003"],
            "ltsc004"=>$data["ltsc004"],
            "cLtaCode"=>$admin->code,
            "uLtaCode"=>$admin->code,
            "uLtarCode"=>$role->code,
        ]);
        if ($ConfigId > 0) {
            Cache::delete("system_config_cache");
            return successReturn("添加成功");
        } else {
            return errorReturn("添加失败");
        }
    }

    /**
     * 编辑字段
     * <p>
     *     1. 验证字段是否存在
     *     2. 如果修改了字段名称 验证字段名称是否已存在
     *     3. 修改字段
     *     4. 删除缓存
     * </p>
     * @param Request $request
     * @return array|string
     */
    public function systemConfigEditAction(Request $request): array|string
    {
        $data = $request->all();
        $has = SystemConfigModel::where([
            ['code', '=', $data["code"]],
            ['id', '=', $data["id"]],
            ['ltsc003', '=', $data["ltsc003"]],
            ['ltsc004', '=', $data["ltsc004"]],
        ])->first();
        if (!$has)
        {
            return errorReturn("字段不存在");
        }
        if ($has->ltsc001 != $data["ltsc001"]) {
            $isLTSC001Has = (new SystemConfigModel)->hasData(dataToWhere([
                "ltsc001"=>$data["ltsc001"],
                "ltsc003"=>$data["ltsc003"],
                "ltsc004"=>$data["ltsc004"],
            ]));
            if ($isLTSC001Has) {
                return errorReturn("分组下字段已存在");
            }
        }
        $loginInfo = $this->getLoginInfo($request);
        $admin = $loginInfo['admin'];
        $role = $loginInfo['role'];
        $update = (new SystemConfigModel)->updateInfo(dataToWhere([
            "code"=>$has->code,
            "version"=>$has->version,
        ]), [
            "ltsc001"=>$data["ltsc001"],
            "ltsc002"=>$data["ltsc002"],
            "uLtaCode"=>$admin->code,
            "uLtarCode"=>$role->code,
        ]);
        if ($update)
        {
            Cache::delete("system_config_cache");
            return successReturn("操作成功");
        }
        return errorReturn("操作失败");
    }

}
